﻿using Songhay.DataAccess.Models;
using Songhay.DataAccess.Models.Extensions;
using Songhay.Mvvm.Models;
using Songhay.Extensions;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;

namespace Songhay.Mvvm.ViewModels
{
    using Extensions;

    /// <summary>
    /// GenericWeb Segment for MVVM
    /// </summary>
    [MetadataType(typeof(SegmentMetadata))]
    public partial class SegmentViewModel : BindableBaseWithValidation, ISegment
    {
        public SegmentViewModel()
        {
            var dataType = typeof(SegmentViewModel);
            this.SupportMetadataTypeAttribute(dataType);
            this.Documents = new ObservableCollection<DocumentViewModel>();
        }

        public SegmentViewModel(Segment data)
            : this()
        {
            if (data == null) throw new ArgumentNullException("The expected data is not here.");
            this._data = data;

            this.UpdateViewModel<Segment, ISegment>(this._data);

            if (this._data.Documents == null) return;
            this._data.Documents.ForEachInEnumerable(i => this.Documents.Add(new DocumentViewModel(i)));

            this.PropertyChanged += (s, args) =>
            {
                this.UpdateModel<ISegment, Segment>(this._data, args.PropertyName);
                var payload = new ModelEventPayload<Segment> { Model = this._data, IsUpdate = true };
                CorePrismEvents.ModelEventForSegment.Publish(payload);
            };
        }

        public string ClientId
        {
            get { return this._clientId; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._clientId, value);
            }
        }

        public DateTime? CreateDate
        {
            get { return this._createDate; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._createDate, value);
            }
        }

        public bool? IsActive
        {
            get { return this._isActive; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._isActive, value);
            }
        }

        public int? ParentSegmentId
        {
            get { return this._parentSegmentId; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._parentSegmentId, value);
            }
        }

        public int SegmentId
        {
            get { return this._segmentId; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._segmentId, value);
            }
        }

        public string SegmentName
        {
            get { return this._segmentName; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._segmentName, value);
            }
        }

        public ObservableCollection<DocumentViewModel> Documents
        {
            get { return this._documents; }
            set { this.SetProperty(ref this._documents, value); }
        }

        public byte? SortOrdinal
        {
            get { return this._sortOrdinal; }
            set
            {
                this.ValidateProperty(this, value);
                this.SetProperty(ref this._sortOrdinal, value);
            }
        }

        public override string ToString()
        {
            return this.ToDisplayText();
        }

        internal void Validate()
        {
            this.ValidateInstance(this);
        }

        string _clientId;
        DateTime? _createDate;
        bool? _isActive;
        int? _parentSegmentId;
        int _segmentId;
        string _segmentName;
        byte? _sortOrdinal;

        Segment _data;
        ObservableCollection<DocumentViewModel> _documents;
    }
}
